<?php

require_once('foursquare_secrets.php');
// get $code from QUERY_STRING
parse_str($_SERVER['QUERY_STRING'], $query);
$code = $query['code'];

// build url
$url = 'https://foursquare.com/oauth2/access_token';
$url .= '?client_id=' . CLIENT_ID;
$url .= '&client_secret=' . CLIENT_SECRET;
$url .= '&grant_type=authorization_code';
$url .= '&redirect_uri=http://localhost/obscure-spire-7574/test/foursquare_callback.php'; //change to your 4sq callback
$url .= '&code=' . $code;

// call to https://foursquare.com/oauth2/access_token with $code
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);

// $result value is json {access_token: ACCESS_TOKEN}
$values = json_decode($result, true);
$token = $values['access_token'];
print("Token from foursquare: " . $token);
// set access_token cookie (if you wish)
$expire = time() + 2592000; // 30 days from now
setcookie("foursquare_token", $token, $expire, '/');

// crosswindow scripting to pass back $token
echo('<script type="text/javascript">');
echo('opener.set4sqKey("' . $token . '");');
echo('self.close();'); // close self
echo('</script>');
?>
